package com.xueqiu.movieplaymanagersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xueqiu.movieplaymanagersystem.entity.Blog;
import com.xueqiu.movieplaymanagersystem.entity.Type;
import com.xueqiu.movieplaymanagersystem.mapper.TypesMapper;
import com.xueqiu.movieplaymanagersystem.service.BlogService;
import com.xueqiu.movieplaymanagersystem.service.TypesService;
import com.xueqiu.movieplaymanagersystem.utils.UUIDUtils;

import java.util.List;

/**
 * @author xueqiu
 */
@Service("typesService")
@Slf4j
public class TypesServiceImpl implements TypesService {
    @Autowired
    private TypesMapper typesMapper;
    @Autowired
    private BlogService blogService;

    /**
     * 新增
     *
     * @param originData 数据源
     * @return 新增对象；含ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Type save(Type originData) {
        //先查询数据库中是否已存在
        Type originType = typesMapper.selectByName(originData.getTypeName());

        if (originType != null) {
            return null;
        }
        //id是使用UUID生成而不是自增
        String uuid = UUIDUtils.uuid();
        originData.setId(uuid);
        log.info("TypesServiceImpl save originData ----> {}",originData);
        return typesMapper.insert(originData) >= 1 ? originData : null;
    }

    /**
     * 编辑
     *
     * @param id   id字段
     * @param name TAG/TYPE名称
     * @return 编辑成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(String id, String name) {
        int update = typesMapper.update(id, name);
        log.info("TypesServiceImpl edit id name ----> {},{}",id,name);
        return update >= 1;
    }

    /**
     * 删除
     *
     * @param id id字段
     * @return 删除成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(String id) {
        int delete = typesMapper.deleteById(id);
        return delete >= 1;
    }

    /**
     * 批量删除
     *
     * @param idList id字段列表
     * @return 删除条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByNameBatch(List<String> idList) {
        return typesMapper.deleteByIdBatch(idList);
    }

    /**
     * 分页查询，条件查询
     *
     * @param keyWord 关键字
     * @param page    当前页
     * @param limit   每页条数
     * @return 结果可能为empty
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Type> findByKeyWord(String keyWord, int page, int limit) {
        if (page == -1 || limit == -1) {
            List<Type> originList = typesMapper.selectByNameKeyWord(keyWord);
            
            PageInfo<Type> resultPageInfoInfo = new PageInfo<>(originList);
            resultPageInfoInfo.setTotal(originList.size());
            return resultPageInfoInfo;
        }
        PageInfo<Type> resultPageInfo = null;
        try (Page<Type> originPage = PageHelper.startPage(page, limit)) {
            typesMapper.selectByNameKeyWord(keyWord);

            List<Type> originList = originPage.getResult();
            resultPageInfo = new PageInfo<>(originList);
        }
        log.info("TypesServiceImpl findByKeyWord resultPageInfo ----> {}",resultPageInfo);
        return resultPageInfo;
    }

    /**
     * 查询；根据类别ID
     *
     * @param typeId 类别ID
     * @return 单个查询结果；可能为NULL
     */
    @Override
    @Transactional(readOnly = true)
    public Type findById(String typeId) {
        Type type = typesMapper.selectById(typeId);
        log.info("TypesServiceImpl findById type ----> {}",type);
        return type;
    }

    /**
     * 查询；包含每个类型下的新闻数量(blog_count)
     * 按照新闻数量倒序排序，取前6个结果
     *
     * @return 最多6个TYPE对象；可能为EMPTY
     */
    @Override
    @Transactional(readOnly = true)
    public List<Type> findWithCountTopSix() {
        return typesMapper.selectWithCountTopSix();
    }

    /**
     * 查询新闻；根据类别ID
     *
     * @param typeId 类别ID
     * @param page   当前页
     * @param limit  每页条数
     * @return 可能为EMPTY
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Blog> findBlogByTypeId(String typeId, int page, int limit) {
        return blogService.findByTypeId(typeId, page, limit);
    }

    /**
     * 查询；类别总数量
     *
     * @return 可能为0
     */
    @Override
    @Transactional(readOnly = true)
    public Integer findCount() {
        Integer count = typesMapper.selectCount();
        log.info("TypesServiceImpl findCount count ----> {}",count);
        return count;
    }
}
